<?php
declare(strict_types=1);

namespace Dao;
// hook dao_sys_cate_use.php

use Field\QuestionThreadConst;
use Field\SysArticleConst;
use Field\SysCateConst;
use Field\SysPagesConst;
use Model\SysCateModel;

class SysCateDao extends SysCateModel
{
    // hook dao_sys_cate_public_start.php
    public array $DataPinyin = [];
    public array $DataKv = [];
    public array $DataKey = [];
    public array $DataGroup = [];
    public array $DataSuperior = [];
    public array $DataSon = [];

    protected array $add_rule = [];
    protected array $edit_rule = [];

    protected array $rule = [
        /*
        "mobile"=>[
            "max"=>3
            "min"=>3
            "func"=>'isMobile'
            "error"=>false,
            'message'=>'格式有误'
        ],
        "username"=>[
            "max"=>3
            "min"=>3
            "func"=>'checkS'
            "error"=>false,
            'message'=>'格式有误'
        ],
        */
    ];
    //数据，字段，验证规则
    /*
    function isMobile($data,$field,$val){

        return true;
    }
    */

    // hook dao_sys_cate_public_end.php


    public function auto_load(): void
    {
        $this->reload();
    }

    public function superior($CateID)
    {
        return $this->DataSuperior[$CateID];
    }

    public function GetParentPinyin($Cate)
    {
        if ($Cate['ParentCateID']) {
            return $this->GetParentPinyin($this->DataKey[$Cate['ParentCateID']]);
        }
        return '/' . $Cate['Pinyin'];
    }

    public function reload()
    {
        // hook model_cate_reload_start.php
        $cache = $this->select();
        $cate_access = $this->SysCateAccess->select();
        $cate_access = arrlist_group($cate_access, 'CateID');

        $_cate = [];
        foreach ($cache as $k => $row) {
            $cache[$k]['access'] = isset($cate_access[$row['CateID']]) ? arrlist_change_key($cate_access[$row['CateID']], 'RoleID') : [];
            $_cate[] = ['CateID' => $row['CateID'], 'ParentCateID' => $row['ParentCateID']];
        }
        // hook model_cate_reload_before.php

        $cate_s_p = arrlist_key_values($_cate, 'CateID', 'ParentCateID');
        $this->DataSuperior = arraylist_superior($cate_s_p);
        $this->DataSon = arraylist_son($cate_s_p);
        $this->DataKv = arrlist_key_values($cache, 'CateID', 'Name');
        $this->DataKey = arrlist_change_key($cache, 'CateID');

        foreach ($cache as $k => $row) {
            $cache[$k]['Url'] = $this->GetParentPinyin($row);
        }
        $this->DataPinyin = arrlist_key_values($cache, 'Url', 'CateID');
        $this->DataKey = arrlist_change_key($cache, 'CateID');
        $DataGroup = arrlist_group($cache, 'GroupAt');
        foreach ($DataGroup as $_k => $_v) {
            $DataGroup[$_k] = arraylist_tree($_v, 'CateID', 'ParentCateID');
        }
        $this->DataGroup = $DataGroup;
        // hook model_cate_reload_after.php
        return $cache;
    }

    /**
     * 根据分类ID获取列表
     * @param $CateID
     * @param string $order_by
     * @param $num
     * @return array
     */
    public function GetListByCateID(int $CateID, int $num = 10, string $order_by = '')
    {
        $data['Cate'] = $this->DataKey[$CateID];
        //var_dump($CateID,$data);
        $CacheKey = 'GetList:' . $data['Cate']['GroupAt'] . ':CateID:' . $CateID . ':' . $num;
        switch ($data['Cate']['GroupAt']) {
            case 'article':
                $List = $this->CacheGet($CacheKey);
                if (empty($List)) {
                    if (!empty($this->DataSon[$CateID])) {
                        $CateIDS = $this->DataSon[$CateID];
                        $CateIDS[] = $CateID;
                    } else {
                        $CateIDS = $CateID;
                    }
                    $List = $this->SysArticle->select(['CateID' => $CateIDS], [$order_by ?: SysArticleConst::ArticleID => -1], 1, $num);
                    $data['List'] = !empty($List) ? arrlist_sort_by_many_field($List, 'Rank', SORT_DESC) : [];
                    $this->CacheSet($CacheKey, $data);
                } else {
                    $data = $List;
                }

                break;
            case 'docs':
            case 'pages':
                $List = $this->CacheGet($CacheKey);
                if (empty($List)) {
                    if (!empty($this->DataSon[$CateID])) {
                        $CateIDS = $this->DataSon[$CateID];
                        $CateIDS[] = $CateID;
                    } else {
                        $CateIDS = $CateID;
                    }
                    $List = $this->SysPages->select(['CateID' => $CateIDS], [$order_by ?: SysPagesConst::PageID => -1], 1, $num);
                    $data['List'] = !empty($List) ? arrlist_sort_by_many_field($List, 'Rank', SORT_DESC) : [];
                    $this->CacheSet($CacheKey, $data);
                } else {
                    $data = $List;
                }
                break;
            case 'question':
                $List = $this->CacheGet($CacheKey);
                if (empty($List)) {
                    if (!empty($this->DataSon[$CateID])) {
                        $CateIDS = $this->DataSon[$CateID];
                        $CateIDS[] = $CateID;
                    } else {
                        $CateIDS = $CateID;
                    }
                    $List = $this->QuestionThread->select(['CateID' => $CateIDS], [$order_by ?: QuestionThreadConst::ThreadID => -1], 1, $num);
                    $data['List'] = !empty($List) ? arrlist_sort_by_many_field($List, 'Rank', SORT_DESC) : [];
                    $this->CacheSet($CacheKey, $data);
                } else {
                    $data = $List;
                }
                break;
            case 'store':
                $ProductIds = [];
                if (!empty($this->DataSon[$CateID])) {
                    $CateIDS = $this->DataSon[$CateID];
                    $CateIDS[] = $CateID;
                    $pex = $this->StoreProduct->key_cate;
                    $newArray = array_map(function ($item) use ($pex) {
                        return $pex . $item;
                    }, $CateIDS);
                    $Pids = $this->CacheMGet($newArray);
                    foreach ($Pids as $v) {
                        !empty($v) && array_push($ProductIds, ...explode(',', $v));
                    }
                } else {
                    $Pids = $this->CacheGet($this->StoreProduct->key_cate . $CateID);
                    $ProductIds = explode(',', $Pids);
                }

                $ListId = array_map(function ($item) {
                    return $this->StoreProduct->key_id . $item;
                }, $ProductIds);
                $data['List'] = arrlist_sort_by_many_field(array_values($this->CacheMGet($ListId)), 'Rank', SORT_DESC);
                break;
        }
        return $data;
    }

    // hook dao_sys_cate_end.php
}

?>